<?php

class Admin_UsersController extends Zend_Controller_Action {
	
	public function init() {
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			if ($auth->getIdentity ()->role != 'administrator') {
				$this->_helper->redirector ( 'index', 'auth', 'default' );
			}
		} else {
			$this->_helper->redirector ( 'index', 'auth', 'default' );
		}
		$this->_helper->layout->setLayout ( 'admin' );
		/* Initialize action controller here */
	}
	
	public function indexAction() {
		$users_model = new Application_Model_Users ();
		$data = $this->view->Action ( 'leftsidebar', 'Index', 'admin' );
		$this->view->leftsidebar = $data;
	}
	
	public function saveAction() {
		$request = $this->getRequest ();
		$data = $request->getPost ();
		
		if (! empty ( $data )) {
			$users_model = new Application_Model_Users ();
			$patents_model = new Application_Model_Parents ();
			$data_users = array ('username' => $data ['username'], 'password' => MD5 ( $data ['password'] ), 'date_created' => date ( 'Y-m-d', time () ) );
			$user_id = $users_model->save ( $data_users );
			if ($user_id) {
				$data_parents = array ('first_name' => $data ['firstname'], 'last_name' => $data ['lastname'], 'user_id' => $user_id );
				$patents_model->save ( $data_parents );
			}
		}
		$result = array ('login' => true, 'result' => 'success', 'message' => '' );
		die ( json_encode ( $result ) );
	}
	
	public function listusersAction() {
		
		$users_model = new Application_Model_Users ();
		$examp = $_REQUEST ["q"]; //query number
		$page = $_REQUEST ['page']; // get the requested page
		$limit = $_REQUEST ['rows']; // get how many rows we want to have into the grid
		$sidx = $_REQUEST ['sidx']; // get index row - i.e. user click to sort
		$sord = $_REQUEST ['sord']; // get the direction
		$searchOn = $this->Strip ( $_REQUEST ['_search'] );
		
		$wh = '';
		
		if ($searchOn == 'true') {
			$sarr = $this->Strip ( $_REQUEST['filters'] );
			$data = json_decode($sarr);
			$finaldata = $data->rules;
			foreach ( $finaldata as $k => $v ) {
				switch ($v->field) {
					case 'username' :
					case 'role' :
					case 'date_created' :
						$wh .= " AND " . $v->field . " LIKE '" . $v->data . "%'";
						break;
					case 'is_active' :
					case 'id' :
						$wh .= " AND " . $v->field . " = '" . $v->data. "'";
						break;
				}
			}
		}
		$db = Zend_Registry::get ( 'db' );
		$stmt = $db->query ( 'Select count(*) as count from users where id !="" '.$wh.'' );
		$result = $stmt->fetch ();
		$count = $result ['count'];
		if ($count > 0) {
			$total_pages = ceil ( $count / $limit );
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages)
			$page = $total_pages;
		$start = $limit * $page - $limit; // do not put $limit*($page - 1)
		if ($start < 0)
			$start = 0;
		 $query = 'Select * from users where id !="" '.$wh.' ORDER BY '.$sidx.' '.$sord. ' LIMIT '.$start.' , '.$limit;
		$stmt = $db->query ( $query );
		$result = $stmt->fetchAll();
		
		$users_list = array ();
		foreach ( $result as $user ) {
			$users_list [] = array ('id' => $user['id'], 'cell' => array ($user['id'], $user['username'], $user['role'],  $user['date_created'], $user['is_active'] ) );
		}
		
		$total = $total_pages;
		
		$final = array ('page' => $page, 'total' => $total, 'records' => $count, 'rows' => ( array ) $users_list );
		
		$result = array ('login' => true, 'result' => 'success', 'store' => $final );
		die ( json_encode ( $final ) );
	}
	public function Strip($value) {
		if (get_magic_quotes_gpc () != 0) {
			if (is_array ( $value ))
				if ($this->array_is_associative ( $value )) {
					foreach ( $value as $k => $v )
						$tmp_val [$k] = stripslashes ( $v );
					$value = $tmp_val;
				} else
					for($j = 0; $j < sizeof ( $value ); $j ++)
						$value [$j] = stripslashes ( $value [$j] );
			else
				$value = stripslashes ( $value );
		}
		return $value;
	}
	
	function array_is_associative($array) {
		if (is_array ( $array ) && ! empty ( $array )) {
			for($iterator = count ( $array ) - 1; $iterator; $iterator --) {
				if (! array_key_exists ( $iterator, $array )) {
					return true;
				}
			}
			return ! array_key_exists ( 0, $array );
		}
		return false;
	}

}



